﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BFlatCompile
{
	public class Parser
	{
		private List<Tokens> tokenSets;

		public Parser()
		{
			this.tokenSets = new List<Tokens>();
		}

		public void AddTokenSet(Tokens tokens)
		{
			this.tokenSets.Add(tokens);
		}

		public Nodes.Program Parse()
		{
			Dictionary<string, Nodes.Namespace> namespaces = new Dictionary<string, Nodes.Namespace>();
			foreach (Tokens tokens in this.tokenSets)
			{
				Nodes.Namespace ns = Nodes.Namespace.ParseWithNamespaces(tokens, null);
				Nodes.Namespace existing;
				if (namespaces.TryGetValue(ns.Name, out existing))
				{
					existing.Merge(ns);
				}
				else
				{
					namespaces.Add(ns.Name, ns);
				}
			}

			return new Nodes.Program(namespaces.Values);
		}
	}
}
